Java和JavaScript作业展示

复数运算

class Complex {
    private double real;
    private double imag;

    // 构造函数
    public Complex(double real, double imag) {
        this.real = real;
        this.imag = imag;
    }

    // 获取实部
    public double getReal() {
        return real;
    }

    // 获取虚部
    public double getImag() {
        return imag;
    }

    // 加法
    public Complex add(Complex other) {
        double newReal = this.real + other.real;
        double newImag = this.imag + other.imag;
        return new Complex(newReal, newImag);
    }

    // 减法
    public Complex subtract(Complex other) {
        double newReal = this.real - other.real;
        double newImag = this.imag - other.imag;
        return new Complex(newReal, newImag);
    }

    // 乘法
    public Complex multiply(Complex other) {
        double newReal = this.real * other.real - this.imag * other.imag;
        double newImag = this.real * other.imag + this.imag * other.real;
        return new Complex(newReal, newImag);
    }

    // 除法
    public Complex divide(Complex other) {
        double denominator = other.real * other.real + other.imag * other.imag;
        double newReal = (this.real * other.real + this.imag * other.imag) / denominator;
        double newImag = (this.imag * other.real - this.real * other.imag) / denominator;
        return new Complex(newReal, newImag);
    }

    @Override
    public String toString() {
        if (imag == 0) {
            return String.valueOf(real);
        } else if (real == 0) {
            return imag + "i";
        } else if (imag < 0) {
            return real + " - " + (-imag) + "i";
        } else {
            return real + " + " + imag + "i";
        }
    }
}

class Main {
    public static void main(String[] args) {
        Complex c1 = new Complex(1, 2);
        Complex c2 = new Complex(3, 4);

        System.out.println("加法: " + c1.add(c2));
        System.out.println("减法: " + c1.subtract(c2));
        System.out.println("乘法: " + c1.multiply(c2));
        System.out.println("除法: " + c1.divide(c2));
    }
}
            
加法: 4.0 + 6.0i
减法: -2.0 - 2.0i
乘法: -5.0 + 10.0i
除法: 0.44 + 0.08i
            

计算1到n的和和n的阶乘

import java.util.Scanner;

public class Homework2 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.print("请输入一个整数n: ");
        int n = scanner.nextInt();

        // 计算1到n的和
        int sum = 0;
        for (int i = 1; i <= n; i++) {
            sum += i;
        }
        System.out.println("1到" + n + "的和为: " + sum);

        // 学有余力部分:计算n的阶乘
        long factorial = 1;
        for (int i = 1; i <= n; i++) {
            factorial *= i;
        }
        System.out.println(n + "的阶乘为: " + factorial);

        scanner.close();
    }
}
            
请输入一个整数n: 5
1到5的和为: 15
5的阶乘为: 120
            

JavaScript代码

// 第一种方法:使用三层嵌套循环遍历所有可能的组合
for (let gj = 0; gj <= 100; gj++) { 
    for (let mj = 0; mj <= 100; mj++) {
        for (let xj = 0; xj <= 100; xj++) {
            // 写条件
            let tj1 = (gj + mj + xj === 100);
            let tj2 = (gj * 5 + mj * 3 + xj / 3 === 100);
            let tj3 = (xj % 3 === 0);
            if (tj1 && tj2 && tj3) {
                console.log("公鸡:" + gj, "母鸡:" + mj, "小鸡:" + xj);
            }
        }
    }
}

console.log("---------------------------");

// 第二种方法:通过分析问题的数学关系,减少循环的范围
for (let gj = 0; gj <= 20; gj++) { 
    for (let mj = 0; mj <= 33; mj++) {
        let xj = 100 - gj - mj;
        if (xj % 3 === 0 && gj * 5 + mj * 3 + xj / 3 === 100) {
            console.log("公鸡:" + gj, "母鸡:" + mj, "小鸡:" + xj);
        }
    }
}
            

运行结果

公鸡:0 母鸡:25 小鸡:75
公鸡:4 母鸡:18 小鸡:78
公鸡:8 母鸡:11 小鸡:81
公鸡:12 母鸡:4 小鸡:84
---------------------------
公鸡:0 母鸡:25 小鸡:75
公鸡:4 母鸡:18 小鸡:78
公鸡:8 母鸡:11 小鸡:81
公鸡:12 母鸡:4 小鸡:84